package ilincar.systemcore.location;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;

import ilincar.systemcore.tool.ILog;

/**
 * Created by ilincar on 2016/12/13.
 */

public class Gps {


    private final String TAG = "Gps";
    private Location mLocation = null;
    private LocationManager mLocationManager = null;
    private Context mContext = null;

    //每5秒获取一次定位信息
    private final int MIN_TIME = 5000;

    //每20米获取一次定位信息
    private final int MIN_DISTANCE = 20;

    private double mLatitude = 0.0;
    private double mLongitude = 0.0;

    /**
     * 初始化
     * @param context
     */
    public Gps(Context context) {
        mContext = context;
        mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

        mLocation = mLocationManager.getLastKnownLocation(getProvider());
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, mLocationListener);
    }


    // 获取Location Provider
    private String getProvider() {
        // 构建位置查询条件
        Criteria criteria = new Criteria();
        // 查询精度：高
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        // 是否查询海拨：否
        criteria.setAltitudeRequired(false);
        // 是否查询方位角 : 否
        criteria.setBearingRequired(false);
        // 是否允许付费：是
        criteria.setCostAllowed(true);
        // 电量要求：低
        criteria.setPowerRequirement(Criteria.ACCURACY_MEDIUM);
        // 返回最合适的符合条件的provider，第2个参数为true说明 , 如果只有一个provider是有效的,则返回当前provider
        return mLocationManager.getBestProvider(criteria, true);
    }

    public double getLatitude() {
        return mLatitude;
    }

    public double getLongitude() {
        return mLongitude;
    }

    private LocationListener mLocationListener = new LocationListener() {
        // 位置发生改变后调用
        public void onLocationChanged(Location location) {
            if (location != null) {
                mLocation = location;
                mLatitude = mLocation.getLatitude();
                mLongitude = mLocation.getLongitude();
            }
        }

        // provider 被用户关闭后调用
        public void onProviderDisabled(String provider) {
            mLocation = null;
        }

        // provider 被用户开启后调用
        public void onProviderEnabled(String provider) {
                Location l = mLocationManager.getLastKnownLocation(provider);
                if (l != null) {
                    mLocation = l;
                }
        }

        // provider 状态变化时调用
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

    };

    public Location getLocation() {
        return mLocation;
    }

    public void closeLocation() {
        if (mLocationManager != null) {
            if (mLocationListener != null) {
                    mLocationManager.removeUpdates(mLocationListener);
                    mLocationListener = null;
                }
            mLocationManager = null;
        }
    }
}
